package com.mt.roll.wms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.Worker;
import com.mt.roll.core.dal.service.SysDictDataServiceBiz;
import com.mt.roll.core.dal.service.WorkerServiceBiz;
import com.mt.roll.core.enums.WhetherEnum;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdListParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.constants.CodeConstant;
import com.mt.roll.wms.params.WorkerParam;
import com.mt.roll.wms.params.WorkerQueryParam;
import com.mt.roll.wms.service.WorkerService;
import com.mt.roll.wms.utils.CodeUtil;
import com.mt.roll.wms.vo.WorkerVo;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wangxitao
 * @Date 2021/1/7 19:34
 * @Version 1.0
 */
@Service
@Transactional
public class WorkerServiceImpl extends BaseService implements WorkerService {
    @Autowired
    private WorkerServiceBiz workerServiceBiz;
    @Resource
    private SysDictDataServiceBiz sysDictDataServiceBiz;

    /**
     * 获取xx
     *
     * @param idParam 主键参数
     * @return xx
     */
    @Override
    public R<WorkerVo> get(IdParam idParam) {
        Worker worker = workerServiceBiz.getById(idParam.getId());
        Assert.notNull("员工不存在", worker);
        WorkerVo workerVo = WorkerVo.builder().build();

        BeanUtils.copyProperties(worker, workerVo);
        return successful(workerVo);
    }

    /**
     * 获取xx列表
     *
     * @param workerQueryParam xx查询参数
     * @return xx列表
     */
    @Override
    public R<List<WorkerVo>> list(WorkerQueryParam workerQueryParam) {
        return null;
    }

    /**
     * 获取员工分页列表
     *
     * @param workerQueryParam 员工查询参数
     * @return 员工分页列表
     */
    @Override
    public R<PageVo<WorkerVo>> page(WorkerQueryParam workerQueryParam) {
        QueryWrapper<Worker> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(workerQueryParam.getEnabled()), Worker.ENABLED, workerQueryParam.getEnabled())
                .like(StringUtils.isNotBlank(workerQueryParam.getKey()), Worker.NAME, workerQueryParam.getKey())
                .orderByDesc(Worker.CREATE_TIME);
        IPage<Worker> worker =
                workerServiceBiz.page(new Page<>(workerQueryParam.getCurrent(), workerQueryParam.getSize()), wrapper);
        List<WorkerVo> workerVo =com.mt.roll.core.utils.BeanUtils.copyList(worker.getRecords(),WorkerVo.class);

        Page page =new Page();
        page.setCurrent(worker.getCurrent());
        page.setSize(worker.getSize());
        page.setTotal(worker.getTotal());
        page.setRecords(workerVo);
        return successful(new PageVo<>(page, WorkerVo.class));
    }

    /**
     * 获取xx树列表
     *
     * @param workerQueryParam xx查询参数
     * @return xx树列表
     */
    @Override
    public R<List<WorkerVo>> tree(WorkerQueryParam workerQueryParam) {
        return null;
    }

    /**
     * 新增员工
     *
     * @param workerParam 员工参数
     * @return 主键
    */
    @Override
    public R<IdVo> add(WorkerParam workerParam) {
        QueryWrapper<Worker> wrapper = new QueryWrapper<>();
        wrapper.eq(Worker.CODE, workerParam.getCode());
        Assert.eqZero(workerServiceBiz.count(wrapper), "员工编码已经存在");
        Worker worker = new Worker();
        BeanUtils.copyProperties(workerParam, worker);
        setCommonField(worker);
        workerServiceBiz.save(worker);
        return successful(IdVo.builder().id(worker.getId()).build());
    }

    /**
     * 更新员工
     *
     * @param workerParam xx参数
     * @return 主键
     */
    @Override
    public R<IdVo> update(WorkerParam workerParam) {
        Worker worker = workerServiceBiz.getById(workerParam.getId());
        //修改员工编码需要检验新编码是否已存在
        if (!worker.getCode().equals(workerParam.getCode())) {
            QueryWrapper<Worker> wrapper = new QueryWrapper<>();
            wrapper.eq(Worker.CODE, workerParam.getCode());
            Assert.eqZero(workerServiceBiz.count(wrapper), "员工编码已经存在");
        }
        Worker updateWorker = new Worker();
        BeanUtils.copyProperties(workerParam, updateWorker);
        setUpdateCommonField(updateWorker);
        workerServiceBiz.updateById(updateWorker);

        return successful(IdVo.builder().id(worker.getId()).build());
    }

    /**
     * 启用停用
     *
     * @param enabledParam 启停参数
     * @return 主键
     */
    @Override
    public R<IdVo> enabled(EnabledParam enabledParam) {
        return null;
    }

    /**
     * 删除员工
     *
     * @param idParam 主键参数
     * @return 主键
     */
    @Override
    public R<IdVo> delete(IdParam idParam) {
        Worker worker = workerServiceBiz.getById(idParam.getId());
        if (worker == null) {
            return failed("员工不存在");
        }
        //使用更新方式删除，需要把code设置为历史code（后面跟随机数）
        Worker updateWorker = new Worker().setId(worker.getId());
        updateWorker.setCode(worker.getCode() + "_" + RandomUtils.nextInt());
        updateWorker.setValid(WhetherEnum.NO.getValue());
        setUpdateCommonField(updateWorker);
        workerServiceBiz.updateById(updateWorker);
        //更新时逻辑删除字段没有更新，需要再调用一次删除操作
        workerServiceBiz.removeById(updateWorker.getId());
        return successful(IdVo.builder().id(worker.getId()).build());
    }

    /**
     * 批量删除xx
     *
     * @param idListParam 主键列表参数
     * @return 主键列表
     */
    @Override
    public R<IdListVo> delete(IdListParam idListParam) {
        return null;
    }

    @Override
    public R<String> getCode() {
        return successful(CodeUtil.getCode(CodeConstant.WORKER_CODE));
    }
}
